<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 2017/7/19
 * Time: 14:13
 */

/**
 * Created by wanglu on 2017/7/19.
 */
class Template extends AdminBase
{

    function __construct()
    {
        parent::__construct();
        $this->load->service('project_service');
        $this->load->service('project_template_service');
        $this->load->service('lang_service');
    }

    public function add() {
        $pid = $this->input->post("pid");
        $name = $this->input->post("name");
        $template = $this->input->post("template");
        $folder = $this->input->post("folder");
        $filename = $this->input->post("filename");
        $remark =  $this->input->post("remark");
        if ($this->project_template_service->add_template($pid, $name, $template, $folder, $filename, $remark)) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }
    }

    public function update() {
        $id = $this->input->post("id");
        $pid = $this->input->post("pid");
        $name = $this->input->post("name");
        $template = $this->input->post("template");
        $folder = $this->input->post("folder");
        $filename = $this->input->post("filename");
        $remark =  $this->input->post("remark");
        
        if ($this->project_template_service->update_template($id, $pid, $name, $template, $folder, $filename, $remark)) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }
    }

    public function delete() {
        $id = $this->input->post("id");
        if ($this->project_template_service->delete_template($id)) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }
    }


    public function templates()
    {
        $pid = $this->input->get("pid");
        $templates = $this->project_template_service->get_templates_by_pid($pid);

        $project_user_relation = $this->project_service->get_project_user_relation($pid, $this->loginUser['id']);
        $this->showView("tpl_project_template", array('templates' => $templates, 'project_user_relation' => $project_user_relation));
    }


    public function get() {
        $id = $this->input->post("id");
        $template = $this->project_template_service->get_template_by_id($id);
        echo json_encode($template);
    }

    public function  folder() {
        $tid = $this->input->get("tid");
        $pid = $this->input->get("pid");
        if (!empty($tid)) {
            $template = $this->project_template_service->get_template_by_id($tid);

            $folders = json_decode($template['folder'], true);
        } else {
            $folders = array();
        }

        $project = $this->project_service->get_project_by_id($pid);

        $langs = $this->lang_service->get_langs_by_pid($project['id']);


        $show_folder_config = array();
        foreach ($langs as $lang) {

            $find = false;


            if (!empty($folders)) {

                foreach ($folders as $folder) {
                    if ($folder['shorthand'] == $lang['shorthand']) {
                        $find = true;
                        $folder['name'] = $lang['name'];
                        $show_folder_config[] = $folder;
                    }
                }
            }
            
            if (!$find) {
                $show_folder_config[] = array(
                    'name'=> $lang['name'],
                    'folder'=> $lang['shorthand'],
                    'shorthand'=> $lang['shorthand']
                );
            }
        }


        $this->showView("tpl_project_lang_folder_config", array("show_folder_config" => $show_folder_config));

    }



}